<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="stuName">
        <el-input v-model="queryParams.stuName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="stuSex">
        <el-select v-model="queryParams.stuSex" placeholder="请选择性别" clearable>
          <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="民族" prop="stuNation">
        <el-select v-model="queryParams.stuNation" placeholder="请选择民族" clearable>
          <el-option v-for="dict in dict.type.nation" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="学号" prop="stuNumber">
        <el-input v-model="queryParams.stuNumber" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="籍贯" prop="stuNativePlace">
        <el-input v-model="queryParams.stuNativePlace" placeholder="请输入籍贯" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证号" prop="stuIdNumber">
        <el-input v-model="queryParams.stuIdNumber" placeholder="请输入身份证号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出生日期" prop="stuDateBirth">
        <el-date-picker clearable v-model="queryParams.stuDateBirth" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请入党时间" prop="stuApplyPartyTime">
        <el-date-picker clearable v-model="queryParams.stuApplyPartyTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择申请入党时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="确立入党积极分子时间" prop="stuMenbershipActivistsTime">
        <el-date-picker clearable v-model="queryParams.stuMenbershipActivistsTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择确立入党积极分子时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="党校培训结业期数" prop="stuPartytraining">
        <el-input v-model="queryParams.stuPartytraining" placeholder="请输入党校培训结业期数" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="确立发展对象时间" prop="stuDevelopentTargetsTime">
        <el-date-picker clearable v-model="queryParams.stuDevelopentTargetsTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择确立发展对象时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入党时间" prop="stuPartymembershipTime">
        <el-date-picker clearable v-model="queryParams.stuPartymembershipTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择入党时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="转正时间" prop="stuTurnaroundTime">
        <el-date-picker clearable v-model="queryParams.stuTurnaroundTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择转正时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所在党支部" prop="stuPartyBranch">
        <el-input v-model="queryParams.stuPartyBranch" placeholder="请输入所在党支部" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="个人联系方式" prop="stuContactNumber">
        <el-input v-model="queryParams.stuContactNumber" placeholder="请输入个人联系方式" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="班级" prop="stuClass">
        <el-input v-model="queryParams.stuClass" placeholder="请输入班级" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.audit_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="未通过原因" prop="rejectionReason">
        <el-input v-model="queryParams.rejectionReason" placeholder="请输入未通过原因" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门id" prop="deptId">
        <el-input v-model="queryParams.deptId" placeholder="请输入部门id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['building:building:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['building:building:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['building:building:remove']">删除</el-button>
      </el-col>
      <!-- 审核   -->
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-check" size="mini" @click="openAuditDialog" :disabled="multiple"
          v-hasPermi="['system:check']" plain>审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['building:building:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 确保visible是通过sync双向绑定 -->
    <audit-dialog :visible.sync="auditDialogVisible" url="/building/building/check"
      table_name="xg_student_party_building" id_name="stu_id" ref="audit" @submit="handleAuditSubmit" />

    <el-table v-loading="loading" :data="buildingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生序号" align="center" prop="stuId" />
      <el-table-column label="姓名" align="center" prop="stuName" />
      <el-table-column label="性别" align="center" prop="stuSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.stuSex" />
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="stuNation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.nation" :value="scope.row.stuNation" />
        </template>
      </el-table-column>
      <el-table-column label="学号" align="center" prop="stuNumber" />
      <el-table-column label="籍贯" align="center" prop="stuNativePlace" />
      <el-table-column label="身份证号" align="center" prop="stuIdNumber" />
      <el-table-column label="出生日期" align="center" prop="stuDateBirth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stuDateBirth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请入党时间" align="center" prop="stuApplyPartyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stuApplyPartyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="确立入党积极分子时间" align="center" prop="stuMenbershipActivistsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stuMenbershipActivistsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="党校培训结业期数" align="center" prop="stuPartytraining" />
      <el-table-column label="确立发展对象时间" align="center" prop="stuDevelopentTargetsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stuDevelopentTargetsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入党时间" align="center" prop="stuPartymembershipTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stuPartymembershipTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转正时间" align="center" prop="stuTurnaroundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stuTurnaroundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所在党支部" align="center" prop="stuPartyBranch" />
      <el-table-column label="个人联系方式" align="center" prop="stuContactNumber" />
      <el-table-column label="班级" align="center" prop="stuClass" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="未通过原因" align="center" prop="rejectionReason" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="部门id" align="center" prop="deptId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['building:building:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['building:building:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改学生党建对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="stuName">
          <el-input v-model="form.stuName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="stuSex">
          <el-select v-model="form.stuSex" placeholder="请选择性别">
            <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="stuNation">
          <el-select v-model="form.stuNation" placeholder="请选择民族">
            <el-option v-for="dict in dict.type.nation" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学号" prop="stuNumber">
          <el-input v-model="form.stuNumber" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="籍贯" prop="stuNativePlace">
          <el-input v-model="form.stuNativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="身份证号" prop="stuIdNumber">
          <el-input v-model="form.stuIdNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="出生日期" prop="stuDateBirth">
          <el-date-picker clearable v-model="form.stuDateBirth" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请入党时间" prop="stuApplyPartyTime">
          <el-date-picker clearable v-model="form.stuApplyPartyTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择申请入党时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="确立入党积极分子时间" prop="stuMenbershipActivistsTime">
          <el-date-picker clearable v-model="form.stuMenbershipActivistsTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择确立入党积极分子时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="党校培训结业期数" prop="stuPartytraining">
          <el-input v-model="form.stuPartytraining" placeholder="请输入党校培训结业期数" />
        </el-form-item>
        <el-form-item label="确立发展对象时间" prop="stuDevelopentTargetsTime">
          <el-date-picker clearable v-model="form.stuDevelopentTargetsTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择确立发展对象时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入党时间" prop="stuPartymembershipTime">
          <el-date-picker clearable v-model="form.stuPartymembershipTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择入党时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="转正时间" prop="stuTurnaroundTime">
          <el-date-picker clearable v-model="form.stuTurnaroundTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择转正时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所在党支部" prop="stuPartyBranch">
          <el-input v-model="form.stuPartyBranch" placeholder="请输入所在党支部" />
        </el-form-item>
        <el-form-item label="个人联系方式" prop="stuContactNumber">
          <el-input v-model="form.stuContactNumber" placeholder="请输入个人联系方式" />
        </el-form-item>
        <el-form-item label="班级" prop="stuClass">
          <el-input v-model="form.stuClass" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
            <el-option v-for="dict in dict.type.audit_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="未通过原因" prop="rejectionReason">
          <el-input v-model="form.rejectionReason" placeholder="请输入未通过原因" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuilding, getBuilding, delBuilding, addBuilding, updateBuilding } from "@/api/building/building";
import AuditDialog from '@/components/AuditDialog/AuditDialog.vue' // 引入审核子组件

export default {
  name: "Building",
  dicts: ['audit_status', 'sys_user_sex', 'nation'],
  components: { AuditDialog },
  data() {
    return {
      // 控制审核对话框的显示
      auditDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生党建表格数据
      buildingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuName: null,
        stuSex: null,
        stuNation: null,
        stuNumber: null,
        stuNativePlace: null,
        stuIdNumber: null,
        stuDateBirth: null,
        stuApplyPartyTime: null,
        stuMenbershipActivistsTime: null,
        stuPartytraining: null,
        stuDevelopentTargetsTime: null,
        stuPartymembershipTime: null,
        stuTurnaroundTime: null,
        stuPartyBranch: null,
        stuContactNumber: null,
        stuClass: null,
        auditStatus: null,
        rejectionReason: null,
        userId: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stuName: [{
          required: true,
          message: '姓名不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^[\u4e00-\u9fa5]{2,}$/,
          message: '姓名不能包含英文字母',
          trigger: 'blur'
        }],
        stuNumber: [
          {
            required: true,
            message: '学号不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^[ABS]\d{8}$/, // 学号格式: A或B或S开头，后面跟8位数字
            message: '学号格式不正确，必须以A、B或S开头，后面跟8位数字',
            trigger: 'blur'
          }],
        stuIdNumber: [{
          required: true,
          message: '身份证号码不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^(^[1-9]\d{7}(\d{2}[0-9]{3}|[0-9]{3})$|^[1-9]\d{5}(18|19|20)?\d{2}\d{2}\d{2}(\d|X)$)/,
          message: '身份证号码格式不正确',
          trigger: 'blur'
        }],
        stuClass: [
          { required: true, message: "班级不能为空", trigger: "blur" },
          { pattern: /^(电化|电气|物联|计科|软件|智能|微机|硕电信|博电信)\d{4}$/, message: "班级格式不正确，必须以“电化/电气/物联/计科/软件/智能/微机/硕电信/博电信”中一个开头，后面跟四位数字", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开审核对话框
    openAuditDialog() {
      if (this.ids != null) {
        this.auditDialogVisible = true // 显示审核对话框
        this.$refs['audit'].setData(this.ids)
      } else this.$message.error('请选择要审核的信息')
    },
    // 提交审核数据
    async handleAuditSubmit() {
      this.auditDialogVisible = false // 关闭审核对话框
      await this.getList() // 刷新数据列表
    },

    /** 查询学生党建列表 */
    getList() {
      this.loading = true;
      listBuilding(this.queryParams).then(response => {
        this.buildingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stuId: null,
        stuName: null,
        stuSex: null,
        stuNation: null,
        stuNumber: null,
        stuNativePlace: null,
        stuIdNumber: null,
        stuDateBirth: null,
        stuApplyPartyTime: null,
        stuMenbershipActivistsTime: null,
        stuPartytraining: null,
        stuDevelopentTargetsTime: null,
        stuPartymembershipTime: null,
        stuTurnaroundTime: null,
        stuPartyBranch: null,
        stuContactNumber: null,
        stuClass: null,
        auditStatus: null,
        rejectionReason: null,
        userId: null,
        deptId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stuId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生党建";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stuId = row.stuId || this.ids
      getBuilding(stuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生党建";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stuId != null) {
            updateBuilding(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuilding(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stuIds = row.stuId || this.ids;
      this.$modal.confirm('是否确认删除学生党建编号为"' + stuIds + '"的数据项？').then(function () {
        return delBuilding(stuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('building/building/export', {
        ...this.queryParams
      }, `building_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
